package ph.loong.deliveryman.app


import android.app.Application
import android.content.Context
import ph.loong.deliveryman.base.BaseActivity
import java.util.*


/**
 * Author jc.lu.
 * Dates 2018/2/8.
 * Email Tbsupland@163.com.
 * desc
 */

class MyApplication : Application() {



    //定义的成员类似于Java中的静态成员，因为kotlin中没有static成员
    companion object {

        private var application: MyApplication? = null

        fun getAplication(): MyApplication? {
            return application
        }

        var context: Context? = null

        /**
         * Activity集合，来管理所有的Activity
         */
        private var activities: MutableList<BaseActivity>? = null

        /**
         * 结束当前所有Activity
         */
        fun clearActivities() {
            val iterator = activities!!.listIterator()
            var activity: BaseActivity?
            while (iterator.hasNext()) {
                activity = iterator.next()
                activity.finish()

            }
        }

        /**
         * 退出应运程序
         */
        fun quiteApplication() {
            clearActivities()
            System.exit(0)
        }
    }


    override fun onCreate() {
        super.onCreate()
        context = this
        application = this
        activities = LinkedList()
    }


    /**
     * 添加一个Activity
     *
     * @param activity
     */
    fun addActivity(activity: BaseActivity) {
        activities?.add(activity)
    }

    /**
     * 结束一个Activity
     *
     * @param activity
     */
    fun removeActivity(activity: BaseActivity) {
        activities?.remove(activity)
    }


}
